using System;
using UnityEngine;

namespace Voodoo.Sauce.LoadingTime
{
	internal class LoadingTimerManager
	{
		private readonly LoadingTimer _globalTimer = new LoadingTimer(DateTimeOffset.FromUnixTimeMilliseconds(ElapsedTime.GetStartTimestamp()));

		private LoadingTimer _voodooSauceSDKTimer;

		private readonly LoadingTimer _unityTimer = new LoadingTimer(DateTimeOffset.FromUnixTimeMilliseconds(ElapsedTime.GetStartTimestamp()));

		private LoadingTimer _privacyTimer;

		private LoadingTimer _attTimer;

		internal long GetSessionId()
		{
			return GetGlobalLoadingTimeStartTimestamp();
		}

		internal void Pause()
		{
			_globalTimer?.Pause();
			_voodooSauceSDKTimer?.Pause();
			_unityTimer?.Pause();
			_privacyTimer?.Pause();
			_attTimer?.Pause();
		}

		internal void Unpause()
		{
			_globalTimer?.Unpause();
			_voodooSauceSDKTimer?.Unpause();
			_unityTimer?.Unpause();
			_privacyTimer?.Unpause();
			_attTimer?.Unpause();
		}

		internal long GetGlobalLoadingTimeStartTimestamp()
		{
			return _globalTimer.GetStartTimestamp();
		}

		internal long GetGlobalLoadingTimeEndTimestamp()
		{
			return _globalTimer.GetEndTimestamp();
		}

		internal long GetGlobalLoadingTimeDuration()
		{
			return _globalTimer.GetDuration();
		}

		internal long GetRealGlobalLoadingTimeDuration()
		{
			long num = GetGlobalLoadingTimeDuration();
			if (num < 0)
			{
				return -1L;
			}
			long privacyDisplayingTimeDuration = GetPrivacyDisplayingTimeDuration();
			if (privacyDisplayingTimeDuration > 0 && GetPrivacyDisplayingTimeStartTimestamp() >= GetGlobalLoadingTimeStartTimestamp() && GetPrivacyDisplayingTimeEndTimestamp() <= GetGlobalLoadingTimeEndTimestamp())
			{
				num -= privacyDisplayingTimeDuration;
			}
			long attDisplayingTimeDuration = GetAttDisplayingTimeDuration();
			if (attDisplayingTimeDuration > 0 && GetAttDisplayingTimeStartTimestamp() >= GetGlobalLoadingTimeStartTimestamp() && GetAttDisplayingTimeEndTimestamp() <= GetGlobalLoadingTimeEndTimestamp())
			{
				num -= attDisplayingTimeDuration;
			}
			return num;
		}

		internal void StopGlobalLoadingTimer()
		{
			_globalTimer.Stop();
		}

		internal bool IsGlobalLoadingTimerStopped()
		{
			return _globalTimer.IsStopped();
		}

		internal void StartVoodooSauceSDKLoadingTimer()
		{
			if (_voodooSauceSDKTimer == null)
			{
				_voodooSauceSDKTimer = new LoadingTimer();
			}
		}

		internal long GetVoodooSauceSDKLoadingTimeStartTimestamp()
		{
			return _voodooSauceSDKTimer?.GetStartTimestamp() ?? 0;
		}

		internal long GetVoodooSauceSDKLoadingTimeEndTimestamp()
		{
			return _voodooSauceSDKTimer?.GetEndTimestamp() ?? 0;
		}

		internal void StopVoodooSauceSDKLoadingTimer()
		{
			_voodooSauceSDKTimer?.Stop();
		}

		internal long GetVoodooSauceSDKLoadingTimeDuration()
		{
			return _voodooSauceSDKTimer?.GetDuration() ?? (-1);
		}

		internal long GetVoodooSauceSDKRealLoadingTimeDuration()
		{
			long num = GetVoodooSauceSDKLoadingTimeDuration();
			if (num < 0)
			{
				return -1L;
			}
			long privacyDisplayingTimeDuration = GetPrivacyDisplayingTimeDuration();
			if (privacyDisplayingTimeDuration > 0 && GetPrivacyDisplayingTimeStartTimestamp() >= GetVoodooSauceSDKLoadingTimeStartTimestamp() && GetPrivacyDisplayingTimeEndTimestamp() <= GetVoodooSauceSDKLoadingTimeEndTimestamp())
			{
				num -= privacyDisplayingTimeDuration;
			}
			long attDisplayingTimeDuration = GetAttDisplayingTimeDuration();
			if (attDisplayingTimeDuration > 0 && GetAttDisplayingTimeStartTimestamp() >= GetVoodooSauceSDKLoadingTimeStartTimestamp() && GetAttDisplayingTimeEndTimestamp() <= GetVoodooSauceSDKLoadingTimeEndTimestamp())
			{
				num -= attDisplayingTimeDuration;
			}
			return num;
		}

		internal long GetUnityLoadingTimeStartTimestamp()
		{
			return _unityTimer.GetStartTimestamp();
		}

		internal long GetUnityLoadingTimeEndTimestamp()
		{
			return _unityTimer.GetEndTimestamp();
		}

		internal long GetUnityLoadingTimeDuration()
		{
			return _unityTimer.GetDuration();
		}

		internal void StopUnityLoadingTimer()
		{
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			if ((double)realtimeSinceStartup > 0.0)
			{
				_unityTimer.SetEnd(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(realtimeSinceStartup)));
			}
		}

		internal void StartPrivacyDisplayingTimer()
		{
			if (_privacyTimer == null)
			{
				_privacyTimer = new LoadingTimer();
			}
		}

		internal void StopPrivacyDisplayingTimer()
		{
			_privacyTimer?.Stop();
		}

		internal long GetPrivacyDisplayingTimeStartTimestamp()
		{
			return _privacyTimer?.GetStartTimestamp() ?? 0;
		}

		internal long GetPrivacyDisplayingTimeEndTimestamp()
		{
			return _privacyTimer?.GetEndTimestamp() ?? 0;
		}

		internal long GetPrivacyDisplayingTimeDuration()
		{
			return _privacyTimer?.GetDuration() ?? (-1);
		}

		internal void StartAttDisplayingTimer()
		{
			if (_attTimer == null)
			{
				_attTimer = new LoadingTimer();
			}
		}

		internal void StopAttDisplayingTimer()
		{
			_attTimer?.Stop();
		}

		internal long GetAttDisplayingTimeStartTimestamp()
		{
			return _attTimer?.GetStartTimestamp() ?? 0;
		}

		internal long GetAttDisplayingTimeEndTimestamp()
		{
			return _attTimer?.GetEndTimestamp() ?? 0;
		}

		internal long GetAttDisplayingTimeDuration()
		{
			return _attTimer?.GetDuration() ?? (-1);
		}
	}
}
